/**
 *Project: jh-framework
 *File: package cn.xkshow.io.upload.FtpUpload.java 
 *Author: admin@xkshow.cn with the copyrite
 ****************************************************************
 *   VERSION       DATE            BY       CHANGE/COMMENT
 * --------------------------------------------------------------
 *    1.0        2013-1-27        xkshow.cn         create
 ****************************************************************
 *note:
 ***************************************************************/ 
package cn.xkshow.io.upload;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.io.IOUtils;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;


public class FtpUpload {

	private String userName;
	private String userPass;
	private String serverIp;
	private int serverPort;
	private FTPClient client;

	public FtpUpload(String serverIp, int serverPort) {
		this.serverIp = serverIp;
		this.serverPort = serverPort;
	}

	/**
	 * 娑撳﹣绱堕弬鍥︽
	 * 
	 * @param localFile
	 *            閺堫剙婀撮弬鍥︽
	 * @param remotePath
	 *            鏉╂粎鈻肩捄顖氱窞
	 * @throws IOException
	 */
	public void upload(String localFileName, String remotePath) throws IOException {
		int index = localFileName.lastIndexOf("/");
		String remoteFileName = localFileName.substring(index+1,localFileName.length());
		upload(new BufferedInputStream(new FileInputStream(localFileName)), remotePath, remoteFileName);
	}

	/**
	 * 娑撳﹣绱堕弬鍥︽
	 * @param inputStream
	 * @param remotePath
	 * @param remoteFileName
	 */
	public void upload(InputStream inputStream, String remotePath, String remoteFileName) throws IOException {
		try{
			client.changeWorkingDirectory(remotePath);
//	        client.setBufferSize(1024);
//	        client.setControlEncoding("UTF-8");
	        client.setFileType(FTP.BINARY_FILE_TYPE);
	        client.storeFile(remoteFileName, inputStream);
		}finally{
			IOUtils.closeQuietly(inputStream);
		}
	}
	/**
	 * 娑撳娴囬弬鍥︽
	 * 
	 * @param remoteFileName
	 *            鏉╂粎鈻奸弬鍥︽
	 * @param localFileName
	 *            閺堫剙婀撮弬鍥︽
	 * @throws IOException
	 */
	public void download(String remoteFileName, String localFileName)
			throws IOException {
		OutputStream out = null;
        try {
        	out = new BufferedOutputStream(new FileOutputStream(localFileName));
//            client.setBufferSize(1024);
            client.setFileType(FTP.BINARY_FILE_TYPE);
            client.retrieveFile(remoteFileName, out);
            out.flush();
        } catch (IOException e) {
        	throw e;
        } finally {
            IOUtils.closeQuietly(out);
        } 
	}

	public void close() {
		if(null != client){
			try {
				client.disconnect();
			} catch (IOException e) {
				e.printStackTrace();
			}
			client = null;
		}
	}

	/**
	 * 閸掓繂顬婇崠鏍懠閹猴拷
	 * 
	 * @throws IOException
	 */
	public void connect() throws IOException {
		client = new FTPClient();
//		client.setControlEncoding("UTF-8");
		client.setDefaultPort(serverPort);
		client.connect(serverIp);
		client.login(userName, userPass);
		client.setDefaultPort(serverPort);
		int reply = client.getReplyCode();
		if(!FTPReply.isPositiveCompletion(reply)) {
			client.disconnect();
			throw new IOException("FTP server refused connection.");
		}
		client.enterLocalPassiveMode();
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getUserPass() {
		return userPass;
	}

	public void setUserPass(String userPass) {
		this.userPass = userPass;
	}
	/**
	 * 濡拷鐓￠弰顖氭儊鐎涙ê婀弬鍥︽
	 * @param fileName
	 * @return 鐎涙ê婀稉搴℃儊
	 * @throws IOException 
	 */
	public boolean exists(String fileName) throws IOException{
		FTPFile[] files = client.listFiles(fileName);
		try {
			return files.length == 1;
		} finally {
			files = null;
		}
	}
}

